package com.passion.meizi;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * 妹子图批量下载
 * 2015-5-29
 * @author passion
 *
 */
public class MeizituDownload
{

	//妹子图页面地址前缀
	private static final String DOWNLOAD_URL="http://www.mzitu.com/share/comment-page-";
	//图片存放路径
	private static final String savePath="E:\\妹子图\\";
	
	private static final Log log=LogFactory.getLog(MeizituDownload.class);
	public static void main(String[] args) throws Exception
	{
			new MeizituDownload().download();

	}

	private void download() throws Exception
	{
		String html=null;
		String[] imgUrls=null;
		File file =new File(savePath);
		if(!file.exists())
			file.mkdirs();
		for(int i=1;i<142;i++)
		{
			html=getHtml(DOWNLOAD_URL+i);
			imgUrls=parseImgUrl(html);
			new Thread(new DownloadImgThread(imgUrls)).start();
		}
		
	}
	/**
	 * 获得妹子图页面html字符串，供后续分析其中的妹子img地址
	 * @param url
	 * @return
	 */
	public String getHtml(String url) 
	{
		log.info("正在获取页面:"+url);
		HttpGet httpGet=new HttpGet(url);
		CloseableHttpClient httpClient=HttpClients.createDefault();
		try
		{
			httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
			CloseableHttpResponse response= httpClient.execute(httpGet);
			try{
				if(response.getStatusLine().getStatusCode()==200)
				{
					HttpEntity entity=response.getEntity();
					if(entity!=null)
					{
						return EntityUtils.toString(entity,"UTF-8");
					}
				}
			}
			catch(IOException e){
				e.printStackTrace();
			}
			finally{
				response.close();
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally{
			try
			{
				httpClient.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 分析html字符串中的妹子图片地址
	 * @param html
	 * @return 返回的数组包含妹子图片地址
	 */
	private String[] parseImgUrl(String html)
	{
		log.info("正在分析图像地址...");
		if(html==null) return null;
		Document doc=Jsoup.parse(html);
		Elements imgElements=doc.select("#comments img");
		final String[] img_urls=new String[imgElements.size()];
		for(int i=0;i<imgElements.size();i++) {
			img_urls[i] = imgElements.get(i).attr("src");
        }
		return img_urls;
	}
	
	//下载图片线程
	class  DownloadImgThread implements Runnable
	{
		private String[] imgUrls;
		public DownloadImgThread(String[] imgUrls)
		{
			this.imgUrls=imgUrls;
		}
		@Override
		public void run()
		{
			if(imgUrls!=null)
			{
				for(String imgUrl:imgUrls)
					downloadImg(imgUrl);
			}
		}
		
		public void downloadImg(String imgUrl)
		{
			log.info("正在下载妹子图："+imgUrl);
			 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).build();  
			HttpGet httpGet=new HttpGet(imgUrl);
			httpGet.setConfig(requestConfig);
			CloseableHttpClient httpClient=HttpClients.createDefault();
			try
			{
				CloseableHttpResponse response= httpClient.execute(httpGet);
				try{
					if(response.getStatusLine().getStatusCode()==200)
					{
						HttpEntity entity=response.getEntity();
						if(entity!=null)
						{
							File f=new File(savePath+System.currentTimeMillis()+".jpg");
							entity.writeTo(new FileOutputStream(f));
							log.info("保存图片到："+f.getPath());
						}
					}
				}
				catch(IOException e){
					e.printStackTrace();
				}
				finally{
					response.close();
				}
				Thread.sleep(800L);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			finally{
				try
				{
					httpClient.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}


